#pragma once

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cerrno>
#include <cassert>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "log.hpp"
using namespace std;

#define PATH_NAME "/home/zty"
#define PROJ_ID 1
#define SHM_SIZE 4096 //共享内存的大小必须是页大小(4096)的整数倍！
#define FIFO_PATH "./fifo"
#define MODE 0666

struct Init{
    Init(){
        umask(0);
        int ret = mkfifo(FIFO_PATH, MODE);
        if(ret == -1)
        {
            perror("mkfifo");
            exit(errno);
        }
        PrintLog("creat fifo success!", DEBUG);
    }
    ~Init(){
        unlink(FIFO_PATH);
        PrintLog("remove fifo success!", DEBUG);
    }
};

int Openfifo(const char* path, int mode){
    int fd = open(path, mode);
    if(fd == -1)
    {
        perror("open");
        exit(errno);
    }
    PrintLog("open fifo success!", DEBUG);
    return fd;
}

void Wait(int fd){
    uint32_t temp = 0;
    ssize_t sz = read(fd, &temp, sizeof(uint32_t));
    assert(sz == sizeof(uint32_t));
    (void)sz;
}

void Signal(int fd){
    uint32_t temp = 1;
    ssize_t sz = write(fd, &temp, sizeof(uint32_t));
    assert(sz == sizeof(uint32_t));
    (void)sz;
}

void Closefifo(int fd){
    close(fd);
    PrintLog("close fifo success!", DEBUG);
}